<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Turns encryption on/off on an already connected socket</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.stream-socket-enable-crypto" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">stream_socket_enable_crypto</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7)</p><p class="refpurpose"><span class="refname">stream_socket_enable_crypto</span> &mdash; <span class="dc-title">Turns encryption on/off on an already connected socket</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.stream-socket-enable-crypto-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>stream_socket_enable_crypto</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$stream</code></span>
   , <span class="methodparam"><span class="type">bool</span> <code class="parameter">$enable</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$crypto_type</code></span>
   [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$session_stream</code></span>
  ]] ) : <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></div>


  <p class="simpara">
   Enable or disable encryption on the stream.
  </p>

  <p class="simpara">
   Once the crypto settings are established, cryptography can be turned
   on and off dynamically by passing <strong><code>TRUE</code></strong> or <strong><code>FALSE</code></strong> in the
   <code class="parameter">enable</code> parameter.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.stream-socket-enable-crypto-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">stream</code></dt>

     <dd>

      <p class="para">
       The stream resource.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">enable</code></dt>

     <dd>

      <p class="para">
       Enable/disable cryptography on the stream.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">crypto_type</code></dt>

     <dd>

      <p class="para">
       Setup encryption on the stream.
       Valid methods are
       <ul class="itemizedlist">
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_SSLv2_CLIENT</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_SSLv3_CLIENT</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_SSLv23_CLIENT</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_ANY_CLIENT</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_TLS_CLIENT</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_SSLv2_SERVER</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_SSLv3_SERVER</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_SSLv23_SERVER</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_ANY_SERVER</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_TLS_SERVER</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_TLSv1_0_SERVER</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_TLSv1_1_SERVER</code></strong></span></li>
        <li class="listitem"><span class="simpara"><strong><code>STREAM_CRYPTO_METHOD_TLSv1_2_SERVER</code></strong></span></li>
       </ul>
      </p>
      <p class="para">
       If omitted, the <em>crypto_method</em> context option on
       the stream&#039;s SSL context will be used instead.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">session_stream</code></dt>

     <dd>

      <p class="para">
       Seed the stream with settings from <code class="parameter">session_stream</code>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.stream-socket-enable-crypto-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns <strong><code>TRUE</code></strong> on success, <strong><code>FALSE</code></strong> if negotiation has failed or
   <em>0</em> if there isn&#039;t enough data and you should try again
   (only for non-blocking sockets).
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.stream-socket-enable-crypto-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.6.0</td>
       <td>
        Introduce <strong><code>STREAM_CRYPTO_METHOD_ANY_CLIENT</code></strong>, <strong><code>STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT</code></strong>, <strong><code>STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT</code></strong>, <strong><code>STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT</code></strong>, <strong><code>STREAM_CRYPTO_METHOD_ANY_SERVER</code></strong>, <strong><code>STREAM_CRYPTO_METHOD_TLSv1_0_SERVER</code></strong>, <strong><code>STREAM_CRYPTO_METHOD_TLSv1_1_SERVER</code></strong>, <strong><code>STREAM_CRYPTO_METHOD_TLSv1_2_SERVER</code></strong>.
       </td>
      </tr>

      <tr>
       <td>5.6.0</td>
       <td>
        The <code class="parameter">crypto_type</code> is now optional.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.stream-socket-enable-crypto-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4838">
    <p><strong>Example #1 <span class="function"><strong>stream_socket_enable_crypto()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_socket_client</span><span style="color: #007700">(</span><span style="color: #DD0000">"tcp://myproto.example.com:31337"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errno</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">30</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">"Unable&nbsp;to&nbsp;connect:&nbsp;</span><span style="color: #0000BB">$errstr</span><span style="color: #DD0000">&nbsp;(</span><span style="color: #0000BB">$errno</span><span style="color: #DD0000">)"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Turn&nbsp;on&nbsp;encryption&nbsp;for&nbsp;login&nbsp;phase&nbsp;*/<br /></span><span style="color: #0000BB">stream_socket_enable_crypto</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_CRYPTO_METHOD_SSLv23_CLIENT</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"USER&nbsp;god\r\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"PASS&nbsp;secret\r\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Turn&nbsp;off&nbsp;encryption&nbsp;for&nbsp;the&nbsp;rest&nbsp;*/<br /></span><span style="color: #0000BB">stream_socket_enable_crypto</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /><br />while&nbsp;(</span><span style="color: #0000BB">$motd&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$motd</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
</pre></div>
    </div>
   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.stream-socket-enable-crypto-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><a href="ref.openssl.html" class="xref">OpenSSL 函数</a></li>
    <li class="member"><a href="transports.html" class="xref">所支持的套接字传输器（Socket Transports）列表</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>